package cz.cvut.manatee.client.concordance;

import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.cvut.manatee.client.IntegerTextBox;
import cz.cvut.manatee.client.RadioButtonExtended;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.concordance.CollectedFilterParameters;
import cz.cvut.manatee.shared.concordance.CollectedParameters;
import cz.cvut.manatee.shared.concordance.CollectedSimpleOrCqlParameters;

public class FilterWidget extends ConcordanceFormWidget {
	private final String KEYWORD_POSITIVE = "Pozitivní";
	private final String KEYWORD_NEGATIVE = "Negativní";
	private final String KEYWORD_MARK = "Označit";
	private final String KEYWORD_FIRST = "první";
	private final String KEYWORD_LAST = "poslední";
	private final String KEYWORD_SURROUND_FROM = "V okolí od";
	private final String KEYWORD_SURROUND_TO = "do";
	//private final String KEYWORD_ATTRIBUTE = "Atribut";
	
	private RadioButton positive;
	private RadioButton negative;
	
	private RadioButton first;
	private RadioButton last;
	
	private IntegerTextBox surround_from;
	private IntegerTextBox surround_to;
	
	//private ListBox attribute;
	
	private SimpleOrCqlWidget simpleOrCQL;
	
	FilterWidget() {
		super();
		
		positive = new RadioButtonExtended("type", KEYWORD_POSITIVE, true, true);
		negative = new RadioButtonExtended("type", KEYWORD_NEGATIVE, true, false);
		positive.setValue(true);
		
		first = new RadioButtonExtended("firstlast", KEYWORD_FIRST, true, true);
		last  = new RadioButtonExtended("firstlast", KEYWORD_LAST, true, false);
		first.setValue(true);
		
		surround_from = new IntegerTextBox(5);
		surround_to   = new IntegerTextBox(5);
		surround_from.setWidth("4ex");
		surround_to.setWidth("4ex");
		
		/*attribute = new ListBox();
		attribute.addItem("lemma", "Lemma");
		attribute.addItem("word", "Word");
		attribute.addItem("tag", "Tag");
		attribute.addItem("lc", "lc");
		attribute.addItem("pos", "pos");
		attribute.addItem("k", "k");
		attribute.addItem("g", "g");
		attribute.addItem("c", "c");*/
		
		simpleOrCQL = new SimpleOrCqlWidget();
		
		HorizontalPanel main = new HorizontalPanel();
		main.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		VerticalPanel positiveNegative = new VerticalPanel();
		positiveNegative.add(positive);
		positiveNegative.add(negative);
		main.add(positiveNegative);
		
		main.add(new Label(KEYWORD_MARK));
		
		VerticalPanel firstLast = new VerticalPanel();
		firstLast.add(first);
		firstLast.add(last);
		main.add(firstLast);
		
		main.add(new Label(KEYWORD_SURROUND_FROM));
		main.add(surround_from);
		main.add(new Label(KEYWORD_SURROUND_TO));
		main.add(surround_to);
		
		/*main.add(new Label(KEYWORD_ATTRIBUTE));
		main.add(attribute);*/
		
		main.add(simpleOrCQL);
		
		this.initWidget(main);
		
		
		
		
	}
	
	
	public boolean validate() {
		this.clearError();

		boolean status = simpleOrCQL.validate();

		if (this.surround_from.getIntegerValue() > this.surround_to.getIntegerValue() ) {
			this.surround_from.addStyleName("formError");
			this.surround_to.addStyleName("formError");
			status = false;
		}

		return status;
	}
	
	public void clearError() {
		simpleOrCQL.clearError();
		this.surround_from.removeStyleName("formError");
		this.surround_to.removeStyleName("formError");
	}
	
	public void setLemmatized(boolean lemmatized) {
		this.simpleOrCQL.setLemmatized(lemmatized);
	}
	
	public String getCQL() {
		return this.simpleOrCQL.getCQL();
	}
	
	public String getSimple() {
		return this.simpleOrCQL.getSimple();
	}

	public boolean isLemma() {
		return this.simpleOrCQL.isLemma();
	}

	public boolean isPositive() {
		return this.positive.getValue();
	}

	public boolean isFirst() {
		return this.first.getValue();
	}

	public int getFrom() {
		return this.surround_from.getIntegerValue();
	}

	public int getTo() {
		return this.surround_to.getIntegerValue();
	}

	protected void setPositive(boolean positive) {
		this.positive.setValue(positive);
		this.negative.setValue(!positive);
	}

	protected void setFirst(boolean first) {
		this.first.setValue(first);
		this.last.setValue(!first);
	}

	protected void setFrom(int from) {
		this.surround_from.setValue(""+from);
	}

	protected void setTo(int to) {
		this.surround_to.setValue(""+to);
	}

	public void setAttributes(AttributItem[] attributs) {
		simpleOrCQL.setAttributes(attributs);
	}

	public CollectedParameters getParameters() {
		return new CollectedFilterParameters(isPositive(), isFirst(), getFrom(), getTo(), (CollectedSimpleOrCqlParameters)simpleOrCQL.getParameters());
	}

	public void setParameters(CollectedParameters params) {
		setPositive(((CollectedFilterParameters)params).isPositive());
		setFirst(((CollectedFilterParameters)params).isFirst());
		setFrom(((CollectedFilterParameters)params).getFrom());
		setTo(((CollectedFilterParameters)params).getTo());
		simpleOrCQL.setParameters(((CollectedFilterParameters)params).getSimplOrCqlParameters());
	}
	
}
